//获取应用实例
const app = getApp();
Page({
    data: {
      /* 地图参数 */
      latitude:'',
      longitude: '',
      centerLatitude: 25.27295,
      centerLongitude: 110.28419,
      markers: [],
      customCalloutMarkerIds: [],
      num: 1,
      windowHeight: app.globalData.windowHeight*2,
      pattern: "map",// 默认订单列表为map地图模式，list为列表模式
      systemOrder: true, //系统派单弹窗
		
      currentTab: 2,
      /* 自定义导航栏参数 */
      loading: false,
      color: '#000000',
      background: '#FFFFFF',
      show: true,
      animated: false,
      back: false,
      title: "打包站",
		
      /* 自定义tabbar */
      list: [
        {
          "pagePath": "/page/index/index",
          "iconPath": "/images/index.png",
          "selectedIconPath": "/images/index-selected.png",
          "text": "抢单"
        },
        {
          "pagePath": "/page/order_list/index",
          "iconPath": "/images/timetable.png",
          "selectedIconPath": "/images/timetable-selected.png",
          "text": "订单"
        },
        
        {
          "pagePath": "/page/mine/mine",
          "iconPath": "/images/mine.png",
          "selectedIconPath": "/images/mine-selected.png",
          "text": "我的"
        },
      ],
      
      stationList: [],
    },
	tabChange(e) {
		console.log( e)
		wx.reLaunch({
		  url: e.detail.item.pagePath
		})
	},
	/* 抢单 */
	grabbing_order: function(e){
		console.log("抢单操作");
		wxShowToast("抢单成功");
	},
	/* 跳转回收站详情 */
	callouttap: function (e){
		/* 定义回收站路径 */
		var url = '/page/recycle_bin_detail/index';
		console.log(e);
		wx.navigateTo({
		  url: url,
		  success: function(res) {
		    // 通过eventChannel向被打开页面传送数据
		    res.eventChannel.emit('acceptDataFromOpenerPage', { data: 'test' })
		  }
		})
	},
	
	/* 订单列表切换 */
	changePattern: function(e){
		var list_type = e.currentTarget.dataset.pattern;
		this.setData({
			pattern: list_type,
		})
		console.log(e);
	},
	/* 系统派单弹窗信息 */
	systemOrderMsg: function (){
		var that = this;
	  // setTimeout(function() {
		 //  // wx.hideTabBar();
		 // that.setData({
			//  systemOrder: false,
		 // })
	  // }, 8000);
	},
  onShow: function (e) {
  	var that = this;
  	if (app.globalData.userInfo) {
  		if(!app.globalData.userInfo.rider_apply){
  			app.showApply();
  			return false;
  		}else{
        if(app.globalData.userInfo.rider_apply.status == 0 || app.globalData.userInfo.rider_apply.status == 2){
          app.showApply();
          return false;
        }
      }
  		that.stationList();
  		that.MapContext = wx.createMapContext('myMap');
  	}else{			
  		setTimeout(function () {
  			if (app.globalData.userInfo) {
  				wx.reLaunch({
  					url: '/page/area/index'
  				});
  			}else{
  				app.check(function(ret){}, '/page/area/index');
  			}
  		}, 500)
  	}
  },
  stationList: function () {
    var that = this;
    app.request('/api/timetable.pack/stationList', {}, function (data, ret) {
  		if(that.data.stationList.lenght == data.lenght){
  			that.setData({
  			  stationList: data,
  			});
  		}else{
        wx.reLaunch({
          url: '/page/area/index'
        })
  		}
  		if(data.length>0){
  			that.setData({
  				centerLatitude: data[0].latitude,
  				centerLongitude: data[0].longitude,
  			});
  		}else{
  			that.setData({
  				centerLatitude: that.data.latitude,
  				centerLongitude: that.data.longitude,
  			});
  		}
    }, function (data, ret) {
      app.error(ret.msg);
    });
  },
  stationDetail: function(e){
    console.log(e);
    var id = e.detail.markerId;
    wx.navigateTo({
      url: "/page/recycle_bin_detail/index?id="+id
    })
  },
  /* 获取用户信息*/
  getUserInfo: function(){
    var that = this;
    app.request("/api/timetable.user/userInfo", {}, function (data, ret) {
      // app.success('头像上传成功!');
      app.globalData.userInfo = data.userInfo;
      that.setData({
        userInfo: data.userInfo
      });
    }, function (data, ret) {
      // app.error(ret.msg);
    });
  }
});